//
//  RealTimeDataVC.m
//  RTUApp
//
//  Created by 陈德军 on 2017/5/18.
//  Copyright © 2017年 陈德军. All rights reserved.
//实时数据

#import "RealTimeDataVC.h"
#import "RealTimeDataModel.h"
#import "SearchEquipmentVC.h"
#import "RealTimeDataTableViewCell.h"

@interface RealTimeDataVC ()<UITableViewDelegate,UITableViewDataSource>{
    UIButton *singleRowShowsBut;
    UIButton *intervalBut;
    UIButton *readBut;
    NSString *str6;
    UITextField *intervalDetailF;
    
    NSInteger hhh;
}
/**
 *  table
 */
@property (nonatomic,strong) UITableView *tableV;
/**
 *  数据数组
 */
@property (nonatomic,strong) NSMutableArray<RealTimeDataModel *> *dataSoucer;
/**
 *  定时器
 */
@property (nonatomic,strong) NSTimer *timer;
@end

@implementation RealTimeDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"蓝牙连接断开";
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSoucer = [NSMutableArray new];
    RealTimeDataModel *model = [RealTimeDataModel new];
    model.numberStr = @"序号";
    model.dateStr = @"日期/时间";
    model.buriedDepthStr = @"埋深(m)";
    model.waterTemperatureStr = @"温度(°C)";
    model.voltageStr = @"电压(V)";
    [self.dataSoucer addObject:model];
    
    kHMBlueToothManager.failureBlock = ^(NSString *value) {
        
    };
    kHMBlueToothManager.bluetoothConnectionDisconnect = ^(NSString *value) {
        self.navigationController.navigationBar.barTintColor = KK_MailColor;
        self.navigationItem.title = @"蓝牙连接断开";
        [PopupView showMessageStr:@"蓝牙连接断开"];
    };
    
    [self addTableView];
    [self addBounsView];
    
    kHMBlueToothManager.dataBlock = ^(NSData *value) {
        if (singleRowShowsBut.selected) {
            [self.dataSoucer removeAllObjects];
            hhh = 0;
            RealTimeDataModel *model = [RealTimeDataModel new];
            model.numberStr = @"序号";
            model.dateStr = @"日期/时间";
            model.buriedDepthStr = @"埋深(m)";
            model.waterTemperatureStr = @"温度(°C)";
            model.voltageStr = @"电压(V)";
            [self.dataSoucer addObject:model];
        }
        NSMutableString *str1 = [NSMutableString stringWithFormat:@"%@",value];
        //第一个参数是要删除的字符的索引，第二个是从此位开始要删除的位数
        [str1 deleteCharactersInRange:NSMakeRange(0, 1)];
        [str1 deleteCharactersInRange:NSMakeRange(str1.length-1, 1)];
        //删除字符串中的空格
        NSString *str4 = [str1 stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *str2 = [str4 substringToIndex:2];
        NSString *str3 = [str4 substringFromIndex:str4.length-2];
        
        if ([str2 isEqualToString:@"68"] && [str3 isEqualToString:@"16"]) {
            str6 = str4;
        }else if ([str2 isEqualToString:@"68"]){
            str6 = str4;
        }else if (![str3 isEqualToString:@"16"]){
            str6 = [NSString stringWithFormat:@"%@%@",str6,str4];
        }else{
            str6 = [NSString stringWithFormat:@"%@%@",str6,str4];
            NSLog(@"最后一行%@",str6);
            if (![[str6 substringToIndex:6] isEqualToString:@"681400"] ) {
                return ;
            }
            
            NSMutableString *str7 = [NSMutableString stringWithFormat:@"%@",str6];
            if (str7.length<46) {
                return;
            }
            hhh++;
            [str7 deleteCharactersInRange:NSMakeRange(0, 10)];
            RealTimeDataModel *model = [RealTimeDataModel new];
            model.numberStr = [NSString stringWithFormat:@"%ld",(long)hhh];
            model.dateStr = [NSString stringWithFormat:@"20%@-%@-%@ %@:%@:%@",[NSString stringWithFormat:@"%lu",strtoul([[str7 substringToIndex:2] UTF8String],0,16)],[NSString stringWithFormat:@"%lu",strtoul([[str7 substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16)],[NSString stringWithFormat:@"%lu",strtoul([[str7 substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16)],[NSString stringWithFormat:@"%lu",strtoul([[str7 substringWithRange:NSMakeRange(6, 2)] UTF8String],0,16)],[NSString stringWithFormat:@"%lu",strtoul([[str7 substringWithRange:NSMakeRange(8, 2)] UTF8String],0,16)],[NSString stringWithFormat:@"%lu",strtoul([[str7 substringWithRange:NSMakeRange(10, 2)] UTF8String],0,16)]];
            [str7 deleteCharactersInRange:NSMakeRange(0, 12)];
            NSData *buriedDepthData = [self stringToByte:[str7 substringToIndex:8]];
            float buriedDepth = *(float*)buriedDepthData.bytes;
            model.buriedDepthStr = [NSString stringWithFormat:@"%.1f",buriedDepth];
            [str7 deleteCharactersInRange:NSMakeRange(0, 8)];
            NSData *waterTemperatureDepthData = [self stringToByte:[str7 substringToIndex:8]];
            float waterTemperature = *(float*)waterTemperatureDepthData.bytes;
            model.waterTemperatureStr = [NSString stringWithFormat:@"%.1f",waterTemperature];
            [str7 deleteCharactersInRange:NSMakeRange(0, 8)];
            NSData *voltageData = [self stringToByte:[str7 substringToIndex:8]];
            float voltage = *(float*)voltageData.bytes;
            model.voltageStr = [NSString stringWithFormat:@"%.1f",voltage];
            [str7 deleteCharactersInRange:NSMakeRange(0, 8)];
            [self.dataSoucer addObject:model];
            
            [self.tableV reloadData];
            [self scrollToTableViewBottom];
        }
        
    };
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ConnectionStatus"] isEqualToString:@"连接成功"]) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.48 green:0.69 blue:0.29 alpha:1.00];
        self.navigationItem.title = @"蓝牙连接成功";
    }else{
        self.navigationController.navigationBar.barTintColor = KK_MailColor;
        self.navigationItem.title = @"蓝牙连接断开";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 触发事件
- (void)clickSingleRowShowsButEvent{
    if (singleRowShowsBut.selected) {
        singleRowShowsBut.selected=NO;
        [singleRowShowsBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
    }else{
        singleRowShowsBut.selected=YES;
        [singleRowShowsBut setImage:[UIImage imageNamed:@"autotime"] forState:(UIControlStateNormal)];
    }
}

- (void)clickIntervalButEvent{
    
    if (intervalBut.selected) {
        intervalBut.selected=NO;
        [intervalBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
    }else{
        intervalBut.selected=YES;
        [intervalBut setImage:[UIImage imageNamed:@"autotime"] forState:(UIControlStateNormal)];
    }
    
    if (readBut.selected) {
        [self.timer invalidate];
        [self clickReadButEvent];
        [self clickReadButEvent];
    }
}

- (void)clickReadButEvent{
    SearchEquipmentVC *searchVC = [SearchEquipmentVC new];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ConnectionStatus"] isEqualToString:@"连接成功"]) {
        if (readBut.selected) {
            readBut.selected=NO;
            [readBut setTitle:@"读取" forState:(UIControlStateNormal)];
            [self.timer invalidate];
        }else{
            if (intervalBut.selected) {
                readBut.selected=YES;
                [readBut setTitle:@"暂停" forState:(UIControlStateNormal)];
                self.timer = [NSTimer scheduledTimerWithTimeInterval:[intervalDetailF.text intValue] block:^(NSTimer * _Nonnull timer) {
                    NSData *data = [self stringToByte:@"6802000c037916"];
                    [kHMBlueToothManager sendData:data];
                } repeats:YES];
                
            }else{
                readBut.selected=YES;
                [readBut setTitle:@"暂停" forState:(UIControlStateNormal)];
                self.timer = [NSTimer scheduledTimerWithTimeInterval:2 block:^(NSTimer * _Nonnull timer) {
                    NSData *data = [self stringToByte:@"6802000c037916"];
                    [kHMBlueToothManager sendData:data];
                } repeats:YES];
            }
        }
    }else{
        [self.navigationController pushViewController:searchVC animated:YES];
        [PopupView showMessageStr:@"请先连接蓝牙设备!"];
    }
}

- (void)clickClearButEvent{
    if (self.dataSoucer.count == 1) {
        [PopupView showMessageStr:@"没有数据可以清除了!"];
        return;
    }
    [self.dataSoucer removeAllObjects];
    hhh = 0;
    RealTimeDataModel *model = [RealTimeDataModel new];
    model.numberStr = @"序号";
    model.dateStr = @"日期/时间";
    model.buriedDepthStr = @"埋深(m)";
    model.waterTemperatureStr = @"温度(°C)";
    model.voltageStr = @"电压(V)";
    [self.dataSoucer addObject:model];
    [self.tableV reloadData];
}

- (void)clickTableViewEvent{
    [self.view endEditing:YES];
}
#pragma mark - UITableViewDelegate 代理分组
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSoucer.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     RealTimeDataTableViewCell *cell = [RealTimeDataTableViewCell cellWithtableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.numberLabel.textColor = KK_MailColor;
        cell.dateLabel.textColor = KK_MailColor;
        cell.buriedDepthLabel.textColor = KK_MailColor;
        cell.waterTemperatureLabel.textColor = KK_MailColor;
        cell.voltageLabel.textColor = KK_MailColor;
    }else{
        cell.numberLabel.textColor = [UIColor blackColor];
        cell.dateLabel.textColor = [UIColor blackColor];
        cell.buriedDepthLabel.textColor = [UIColor blackColor];
        cell.waterTemperatureLabel.textColor = [UIColor blackColor];
        cell.voltageLabel.textColor = [UIColor blackColor];
    }
    [cell setModel:self.dataSoucer[indexPath.row]];
    return cell;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


#pragma mark 转换工具
-(NSData*)stringToByte:(NSString*)string
{
    NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"%lu",(unsigned long)[hexString length]);
    if ([hexString length]%2!=0) {
        return nil;
    }
    Byte tempbyt[1]={0};
    NSMutableData* bytes=[NSMutableData data];
    for(int i=0;i<[hexString length];i++)
    {
        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            return nil;
        i++;
        
        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            return nil;
        
        tempbyt[0] = int_ch1+int_ch2;  ///将转化后的数放入Byte数组里
        [bytes appendBytes:tempbyt length:1];
    }
    return bytes;
}



-(NSString *)stringFromHexString:(NSString *)hexString { //
    
    char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
    bzero(myBuffer, [hexString length] / 2 + 1);
    for (int i = 0; i < [hexString length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    //    NSLog(@"------字符串------%@",unicodeString);
    return unicodeString;
}
//把 byte数组转成 Int类型的数据
-(int) lBytesToInt:(NSString *)string
{
    NSString *str1 = [NSString stringWithFormat:@"%lu",strtoul([[string substringToIndex:2] UTF8String],0,16)];
    NSString *str2 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16)];
    NSString *str3 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16)];
    
    int  temp1 = [str1 intValue];
    int  temp2 = [str2 intValue];
    int  temp3 = [str3 intValue];
    Byte byte1[] = {temp1,temp2,temp3};
    
    int height = 0;
    NSData * testData =[NSData dataWithBytes:byte1 length:4];
    for (int i = 0; i < [testData length]; i++)
    {
        if (byte1[[testData length]-i] >= 0)
        {
            height = height + byte1[[testData length]-i];
        } else
        {
            height = height + 256 + byte1[[testData length]-i];
        }
        height = height * 256;
    }
    if (byte1[0] >= 0)
    {
        height = height + byte1[0];
    } else {
        height = height + 256 + byte1[0];
    }
    return height;
}


- (int)hextoint:(NSString *)string
{
    NSString *str1 = [NSString stringWithFormat:@"%lu",strtoul([[string substringToIndex:2] UTF8String],0,16)];
    NSString *str2 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16)];
    NSString *str3 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16)];
    NSString *str4 = [NSString stringWithFormat:@"%lu",strtoul([[string substringWithRange:NSMakeRange(6, 2)] UTF8String],0,16)];
    
    int  temp1 = [str1 intValue];
    int  temp2 = [str2 intValue];
    int  temp3 = [str3 intValue];
    int  temp4 = [str4 intValue];
    Byte dataVal[] = {temp1,temp2,temp3,temp4};
    int reVal = -1;
    
    
    reVal = (int)dataVal[3] << 24;
    
    reVal += (int)dataVal[2] << 16;
    
    reVal += (int)dataVal[1] << 8;
    
    reVal += (int)dataVal[0];
    return reVal;
}

//普通字符串转换为十六进制的。
- (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
        
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        
        else
            
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}





//测试
- (NSMutableArray *)hexToByteArr:(NSString *)str{
    int hexlen = (int)str.length;
    NSMutableArray *array = [NSMutableArray new];
    Byte result[str.length/2];
    if ([self isOdd:hexlen] == 1) {
        ++hexlen;
        str = [NSString stringWithFormat:@"0%@",str];
    }else{
        
    }
    
    int j = 0;
    for (int i = 0; i<hexlen; i+=2) {
        NSString *bstr = [NSString stringWithFormat:@"%lu",strtoul([[str substringWithRange:NSMakeRange(i, 2)] UTF8String],0,16)];
        if ([bstr intValue] >128) {
            bstr = [NSString stringWithFormat:@"%d",[bstr intValue]-256];
        }else if ([bstr intValue]<-128){
            bstr = [NSString stringWithFormat:@"%d",256+[bstr intValue]];
        }
        [array addObject:bstr];
        j++;
    }
    //    NSLog(@"%@",array);
    return array;
}

- (NSString *)getAddtion:(NSMutableArray *)byte{
    int len = byte.count;
    int num = 0;
    for (int i = 0; i<len; i++) {
        num += [byte[i] intValue];
        NSLog(@"基数：%@",[NSString stringWithFormat:@"%d",[byte[i] intValue]]);
        if (num>128) {
            num = num-256;
        }else if (num<-128){
            num = 256+num;
        }
        NSLog(@"求和：%@",[NSString stringWithFormat:@"%d",num]);
    }
    
    if (num<10 && num>0) {
        NSLog(@"--%@",[NSString stringWithFormat:@"0%@",[self ToHex:[[NSString stringWithFormat:@"0%d",num] intValue]]]);
        return [NSString stringWithFormat:@"0%@",[self ToHex:[[NSString stringWithFormat:@"0%d",num] intValue]]];
    }else if (num<0){
        num = 256+num;
        NSLog(@"==%@",[self ToHex:[[NSString stringWithFormat:@"%d",num] intValue]]);
        return [self ToHex:[[NSString stringWithFormat:@"%d",num] intValue]];
    }else{
        NSLog(@"++%@",[self ToHex:[[NSString stringWithFormat:@"%d",num] intValue]]);
        return [self ToHex:[[NSString stringWithFormat:@"%d",num] intValue]];
    }
}

- (int)isOdd:(int )num{
    return num & 1;
}


-(NSString *)ToHex:(long long int)tmpid
{
    NSString *nLetterValue;
    NSString *str =@"";
    long long int ttmpig;
    for (int i = 0; i<9; i++) {
        ttmpig=tmpid%16;
        tmpid=tmpid/16;
        switch (ttmpig)
        {
            case 10:
                nLetterValue =@"A";break;
            case 11:
                nLetterValue =@"B";break;
            case 12:
                nLetterValue =@"C";break;
            case 13:
                nLetterValue =@"D";break;
            case 14:
                nLetterValue =@"E";break;
            case 15:
                nLetterValue =@"F";break;
            default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
                
        }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }
        
    }
    return str;
}

//判断数字是否小于10 如果小于前面补0
- (NSString *)inFrontOfTheFill:(NSString *)str{
    if (str.length == 1) {
        str = [NSString stringWithFormat:@"0%@",str];
    }
    return str;
}

#pragma mark 视图布局
- (void)addTableView{
    self.tableV = [UITableView new];
    self.tableV.size = CGSizeMake(kScreenWidth, (kScreenHeight-208));
    self.tableV.left = 0;
    self.tableV.top = 0;
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    self.tableV.userInteractionEnabled = YES;
    UITapGestureRecognizer *tableVTGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTableViewEvent)];
    [self.tableV addGestureRecognizer:tableVTGR];
    self.tableV.tableFooterView  = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableV registerClass:[RealTimeDataTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableV];
    
}

- (void)addBounsView{
    UIView *baseView = [UIView new];
    baseView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
    baseView.size = CGSizeMake(kScreenWidth, 100);
    baseView.top = self.tableV.bottom;
    baseView.left = 0;
    [self.view addSubview:baseView];
    
    singleRowShowsBut = [UIButton new];
    singleRowShowsBut.selected = NO;
    singleRowShowsBut.top = 10;
    singleRowShowsBut.left = 10;
    singleRowShowsBut.size = CGSizeMake(20, 20);
    [singleRowShowsBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
    [singleRowShowsBut addTarget:self action:@selector(clickSingleRowShowsButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [baseView addSubview:singleRowShowsBut];
    UILabel *singleRowShowsLabel = [UILabel new];
    singleRowShowsLabel.text = @"单行显示";
    singleRowShowsLabel.font = [UIFont systemFontOfSize:13];
    [singleRowShowsLabel sizeToFit];
    singleRowShowsLabel.centerY = singleRowShowsBut.centerY;
    singleRowShowsLabel.left = singleRowShowsBut.right + 5;
    [baseView addSubview:singleRowShowsLabel];
    
    
    intervalBut = [UIButton new];
    intervalBut.selected = NO;
    intervalBut.top = singleRowShowsBut.top;
    intervalBut.left = singleRowShowsLabel.right +40;
    intervalBut.size = CGSizeMake(20, 20);
    [intervalBut setImage:[UIImage imageNamed:@"autoimageff"] forState:(UIControlStateNormal)];
    [intervalBut addTarget:self action:@selector(clickIntervalButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [baseView addSubview:intervalBut];
    UILabel *intervalLabel = [UILabel new];
    intervalLabel.text = @"等间隔召唤";
    intervalLabel.font = [UIFont systemFontOfSize:13];
    [intervalLabel sizeToFit];
    intervalLabel.centerY = intervalBut.centerY;
    intervalLabel.left = intervalBut.right + 5;
    [baseView addSubview:intervalLabel];
    
    UILabel *intervalDetailLabel = [UILabel new];
    intervalDetailLabel.font = [UIFont systemFontOfSize:13];
    intervalDetailLabel.text = @"间隔:";
    intervalDetailLabel.top = intervalLabel.top;
    intervalDetailLabel.left = intervalLabel.right + 40;
    [intervalDetailLabel sizeToFit];
    [baseView addSubview:intervalDetailLabel];
    intervalDetailF = [UITextField new];
    intervalDetailF.keyboardType = UIKeyboardTypeNumberPad;
    intervalDetailF.text = @"6";
    intervalDetailF.textAlignment = NSTextAlignmentCenter;
    intervalDetailF.left = intervalDetailLabel.right + 5;
    intervalDetailF.size = CGSizeMake(50, 20);
    intervalDetailF.centerY = intervalDetailLabel.centerY;
    intervalDetailF.layer.borderWidth = 0.3;
    intervalDetailF.layer.borderColor = [UIColor grayColor].CGColor;
    [baseView addSubview:intervalDetailF];
    UILabel *secondsLabel = [UILabel new];
    secondsLabel.font = [UIFont systemFontOfSize:13];
    secondsLabel.text = @"秒";
    [secondsLabel sizeToFit];
    secondsLabel.top = intervalDetailLabel.top;
    secondsLabel.left = intervalDetailF.right + 3;
    [baseView addSubview:secondsLabel];
    
    UIView *lienView = [UIView new];
    lienView.backgroundColor = [UIColor grayColor];
    lienView.size = CGSizeMake(kScreenWidth, 0.3);
    lienView.left = 0;
    lienView.top = intervalBut.bottom + 10;
    [baseView addSubview:lienView];
    
    readBut = [UIButton new];
    readBut.selected = NO;
    readBut.backgroundColor = KK_MailColor;
    [readBut setTitle:@"读取" forState:(UIControlStateNormal)];
    readBut.size = CGSizeMake(100, 33);
    readBut.left = (kScreenWidth-250)/2;
    readBut.top = lienView.bottom + (60 - readBut.size.height)/2;
    readBut.layer.cornerRadius = 6;
    [readBut addTarget:self action:@selector(clickReadButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [baseView addSubview:readBut];
    
    UIButton *clearBut = [UIButton new];
    clearBut.backgroundColor = KK_MailColor;
    [clearBut setTitle:@"清除" forState:(UIControlStateNormal)];
    clearBut.size = CGSizeMake(100, 33);
    clearBut.left = readBut.right + 50;
    clearBut.top = readBut.top;
    clearBut.layer.cornerRadius = 6;
    [clearBut addTarget:self action:@selector(clickClearButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    [baseView addSubview:clearBut];
}

- (void)scrollToTableViewBottom {
    
    NSInteger s = [self.tableV numberOfSections];  //有多少组
    if (s<1) return;  //无数据时不执行 要不会crash
    NSInteger r = [self.tableV numberOfRowsInSection:s-1]; //最后一组有多少行
    if (r<1) return;
    NSIndexPath *ip = [NSIndexPath indexPathForRow:r-1 inSection:s-1];  //取最后一行数据
    [self.tableV scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:NO]; //滚动到最后一行
    
//    [self.tableV scrollRectToVisible:CGRectMake(0, self.tableV.contentSize.height - 1, self.tableV.bounds.size.width, 1) animated:NO];
}
@end
